<template>
  <div class="container">
    <div class="center-container">
      <el-form :model="userForm" :rules="rules" ref="userForm" label-width="100px"
               class="demo-userForm">
        <span>基本信息：</span>
        <br>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="userForm.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="籍贯" prop="nativePlace">
          <el-select v-model="userForm.nativePlace" placeholder="请选择籍贯">
            <el-option value="110000" label="北京市"></el-option>
            <el-option value="120000" label="天津市"></el-option>
          </el-select>
        </el-form-item>

        <span>联系方式：</span>
        <br>
        <el-form-item label="QQ" prop="contactDetails.qq">
          <el-input v-model="userForm.contactDetails.qq"></el-input>
        </el-form-item>
        <el-form-item label="微信" prop="contactDetails.wx">
          <el-input v-model="userForm.contactDetails.wx"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="contactDetails.phone">
          <el-input v-model="userForm.contactDetails.phone"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('userForm')">保 存</el-button>
          <el-button @click="resetForm('userForm')">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="centerDialogVisible"
      width="30%"
      @close="closeDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      center>
      <span><pre>{{ userForm }}</pre></span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "validate-01",
  data() {
    return {
      userForm: {
        name: '',
        nativePlace: '',
        gender: '',
        contactDetails: {
          qq: '',
          wx: '',
          phone: ''
        }
      },
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: ['blur', 'change']},
          {min: 2, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        nativePlace: [
          {required: true, message: '请选择籍贯', trigger: 'blur'}
        ],
        gender: [
          {required: true, message: '请选择籍贯', trigger: 'change'}
        ],
        'contactDetails.qq': [
          {required: true, message: '请输入联系方式qq', trigger: ['blur', 'change']},
        ],
        'contactDetails.wx': [
          {required: true, message: '请输入联系方式微信', trigger: ['blur', 'change']},
        ],
        'contactDetails.phone': [
          {required: true, message: '请输入联系方式手机号', trigger: ['blur', 'change']},
        ],
      },

      centerDialogVisible: false,
    };
  },
  methods: {
    closeDialog() {
      this.centerDialogVisible = false;
      this.$refs.userForm.resetFields();
    },
    submitForm(formName) {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.centerDialogVisible = true;
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      //this.$refs[formName].resetFields();
      this.$refs.userForm.resetFields();
    }
  }
}
</script>

<style scoped>
.container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 设置父容器的高度为视口高度，使其充满整个屏幕 */
}

.center-container {
  max-width: 600px; /* 设置内部容器的最大宽度，根据需求进行调整 */
}
</style>
